package com.gm.wholesale.util;

public class HexUtil {
    private static final char hexDigits[] =
            {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    /**
     * Bytes to hex string.
     * <p>e.g. bytes2HexString(new byte[] { 0, (byte) 0xa8 }) returns "00A8"</p>
     *
     * @param bytes The bytes.
     * @return hex string
     */


    /**
     * Hex string to bytes.
     * <p>e.g. hexString2Bytes("00A8") returns { 0, (byte) 0xA8 }</p>
     *
     * @param hexString The hex string.
     * @return the bytes
     */
    public static byte[] hexString2Bytes(String hexString) {
        if (isSpace(hexString)) return null;
        int len = hexString.length();
        if (len % 2 != 0) {
            hexString = "0" + hexString;
            len = len + 1;
        }
        char[] hexBytes = hexString.toUpperCase().toCharArray();
        byte[] ret = new byte[len >> 1];
        for (int i = 0; i < len; i += 2) {
            ret[i >> 1] = (byte) (hex2Int(hexBytes[i]) << 4 | hex2Int(hexBytes[i + 1]));
        }
        return ret;
    }

    private static boolean isSpace(final String s) {
        if (s == null) return true;
        for (int i = 0, len = s.length(); i < len; ++i) {
            if (!Character.isWhitespace(s.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    private static int hex2Int(final char hexChar) {
        if (hexChar >= '0' && hexChar <= '9') {
            return hexChar - '0';
        } else if (hexChar >= 'A' && hexChar <= 'F') {
            return hexChar - 'A' + 10;
        } else {
            throw new IllegalArgumentException();
        }
    }


    public static byte[] getStringhex(String ST)
    {
        ST=ST.replaceAll(" ", "");
        //Log.v("getStringhex",ST);
        char[] buffer =ST.toCharArray();
        byte[] Byte = new byte[buffer.length/2];
        int index=0;
        int bit_st=0;
        for(int i=0;i<buffer.length;i++)
        {
            int v=(int)(buffer[i]&0xFF);

            if(((v>47)&&(v<58)) || ((v>64)&&(v<71)) || ((v>96)&&(v<103))){
                if(bit_st==0){
                    //Log.v("getStringhex","F True");
                    Byte[index]|= (getASCvalue(buffer[i])*16);
                    //Log.v("getStringhex",String.valueOf(Byte[index]));
                    bit_st=1;
                }else {
                    Byte[index]|= (getASCvalue(buffer[i]));
                    //Log.v("getStringhex","F false");
                    //Log.v("getStringhex",String.valueOf(Byte[index]));
                    bit_st=0;
                    index++;
                }
            }else if (v==32){
                //Log.v("getStringhex","spance");
                if(bit_st==0);
                else{
                    index++;
                    bit_st=0;
                }
            }else continue;
        }
        bit_st=0;
        return Byte;
    }

    public static byte getASCvalue(char in)
    {
        byte out=0;
        switch(in){
            case '0':out=0;break;
            case '1':out=1;break;
            case '2':out=2;break;
            case '3':out=3;break;
            case '4':out=4;break;
            case '5':out=5;break;
            case '6':out=6;break;
            case '7':out=7;break;
            case '8':out=8;break;
            case '9':out=9;break;
            case 'a':out=10;break;
            case 'b':out=11;break;
            case 'c':out=12;break;
            case 'd':out=13;break;
            case 'e':out=14;break;
            case 'f':out=15;break;
            case 'A':out=10;break;
            case 'B':out=11;break;
            case 'C':out=12;break;
            case 'D':out=13;break;
            case 'E':out=14;break;
            case 'F':out=15;break;
        }
        return out;
    }

}
